{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {
    "collapsed": false
   },
   "outputs": [],
   "source": [
    "import numpy as np\n",
    "import json\n",
    "from keras.models import Model\n",
    "from keras.layers import Input\n",
    "from keras.layers.convolutional import Conv2D\n",
    "from keras.layers.normalization import BatchNormalization\n",
    "from keras import backend as K"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 14,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "def format_decimal(arr, places=8):\n",
    "    return [round(x * 10**places) / 10**places for x in arr]"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### pipeline 3"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 17,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "{'input': {'data': [0.31759583, 0.6885148, -0.68830894, -0.48246991, 0.38722252, -0.71826338, 0.28167336, -0.10631059, 0.57686051, -0.08392634, 0.63169069, 0.92647029, 0.57965537, -0.02421493, -0.80579281, -0.84294672, -0.95541455, 0.65641506, 0.44666966, 0.63373872, 0.70152461, 0.91750748, -0.18567131, -0.10524724, 0.30394866, -0.07507043, -0.66244181, 0.74740359, 0.79405714, 0.70884104, 0.66195359, 0.7746769, 0.66130843, 0.86677627, -0.12720878, -0.43449034, 0.49935716, 0.26704177, -0.89278177, 0.0158887, -0.7238917, -0.23083048, -0.91162145, -0.08276274, -0.87686163, 0.19463103, 0.46543292, 0.46718683, -0.71095003, -0.07916022, -0.19200656, 0.17001561, -0.20880196, -0.7006228, -0.94967571, -0.96566599, 0.88323724, -0.65126492, 0.89773214, 0.92314163, -0.53529897, 0.26889376, -0.49342635, -0.5706408, -0.78567281, 0.87258448, 0.63265736, -0.57089391, 0.4819419, 0.08934675, 0.23480018, 0.67846046, -0.78819246, 0.033911, 0.76888503, -0.52645586, 0.59687518, -0.25687455, 0.79724646, 0.6285436, -0.03638819, 0.19304589, -0.15963308, -0.1037585, -0.50259404, 0.90986773, 0.59710862, -0.83715953, 0.10751754, 0.96915957, -0.93640369, 0.25101012, -0.25732363, -0.48839447, -0.97447067, -0.89939447, -0.56619907, 0.09673349, 0.30361163, 0.56621482, -0.14343406, -0.61274101, 0.1428037, 0.61177701, -0.92481965, -0.43678126, 0.67070456, 0.38354262, 0.08051313, -0.18312295, -0.06412202, -0.25107677, 0.95712223, 0.825422, -0.02931765, -0.95161584, -0.12765254, -0.5808294, -0.09477813, 0.69069514, 0.2633764, -0.29312753, 0.41610649, 0.44635815, 0.43732847, -0.25859129, 0.56476435, -0.93324228], 'shape': [8, 8, 2]}, 'weights': [{'data': [0.31759583, 0.6885148, -0.68830894, -0.48246991, 0.38722252, -0.71826338, 0.28167336, -0.10631059, 0.57686051, -0.08392634, 0.63169069, 0.92647029, 0.57965537, -0.02421493, -0.80579281, -0.84294672, -0.95541455, 0.65641506, 0.44666966, 0.63373872, 0.70152461, 0.91750748, -0.18567131, -0.10524724, 0.30394866, -0.07507043, -0.66244181, 0.74740359, 0.79405714, 0.70884104, 0.66195359, 0.7746769, 0.66130843, 0.86677627, -0.12720878, -0.43449034, 0.49935716, 0.26704177, -0.89278177, 0.0158887, -0.7238917, -0.23083048, -0.91162145, -0.08276274, -0.87686163, 0.19463103, 0.46543292, 0.46718683, -0.71095003, -0.07916022, -0.19200656, 0.17001561, -0.20880196, -0.7006228, -0.94967571, -0.96566599, 0.88323724, -0.65126492, 0.89773214, 0.92314163, -0.53529897, 0.26889376, -0.49342635, -0.5706408, -0.78567281, 0.87258448, 0.63265736, -0.57089391, 0.4819419, 0.08934675, 0.23480018, 0.67846046], 'shape': [3, 3, 2, 4]}, {'data': [-0.3328675, 0.65031726, 0.9955013, -0.45836743], 'shape': [4]}, {'data': [-0.01811637, 0.65911997, 0.7697076, 0.31380283], 'shape': [4]}, {'data': [0.09180953, 0.60345765, -0.60595646, 0.48442452], 'shape': [4]}, {'data': [-0.70276721, -0.81800493, 0.96085533, -0.34024315], 'shape': [4]}, {'data': [0.73499916, 0.66996605, 0.43315305, 0.26612579], 'shape': [4]}], 'expected': {'data': [0.07696919, 1.9980222, -0.56941795, 0.69100457, 0.04104563, 2.20297408, 0.40969431, 2.39508724, 0.07696919, 2.66510081, -1.72839439, 1.09201384, 0.06249164, 3.10122061, -0.12628055, 0.77364504, 0.07696919, 2.0159483, -1.72839439, 0.76308686, 0.06674162, 2.65042186, -0.86940914, 0.69100457, 0.07070555, 2.13343883, 0.16422367, 1.16649079, 0.02672999, 1.26167607, -1.69440162, 0.69100457, 0.07696919, 2.63459206, 0.74974048, 0.69100457, 0.02457088, 2.28845549, 4.81625557, 1.45197022, 0.07696919, 2.10513449, 0.48860824, 0.69100457, 0.07696919, 1.26167607, -1.72839439, 0.69100457, 0.05696189, 1.40567112, 0.46875489, 0.69100457, 0.03308511, 1.56635797, 1.874704, 0.92994905, 0.07696919, 1.26167607, -1.72839439, 0.69100457, 0.07696919, 1.27736473, -1.72839439, 0.69100457, 0.04260247, 1.71712136, -1.10132217, 0.69100457, 0.06886323, 2.61735082, 1.43218982, 1.88393497, 0.07696684, 1.27437556, 0.60538638, 0.69100457, 0.07696919, 1.3356446, -1.72839439, 0.69100457, 0.07696919, 1.79615641, 0.5510174, 1.51647294, 0.07696919, 2.47862291, 0.45873821, 3.02068615, 0.07694453, 2.60288715, -1.72839439, 0.69100457, 0.07356133, 1.26167607, -1.72839439, 0.69100457, 0.07696919, 1.26167607, 2.6666131, 1.46920729, 0.06490703, 2.35775423, -1.72839439, 0.69100457, 0.0490119, 2.22904658, 0.18748093, 0.69100457, 0.07696919, 2.110502, -1.72839439, 0.69100457, 0.06165965, 1.41108954, 3.24514437, 2.51822257, 0.07446862, 2.9166739, 1.11782157, 0.69100457, 0.05211459, 1.57755303, -1.72839439, 0.69100457, 0.07696919, 2.07491684, 1.22092974, 1.60538256, 0.06369025, 2.42108774, 1.22758257, 0.69100457, 0.07051367, 1.5405705, -1.72839439, 0.69100457, 0.07696919, 1.47502732, -0.89822304, 0.69100457, 0.07696919, 1.26167607, 0.29542124, 0.69100457], 'shape': [6, 6, 4]}}\n"
     ]
    }
   ],
   "source": [
    "data_in_shape = (8, 8, 2)\n",
    "\n",
    "conv_0 = Conv2D(4, 3, 3, activation='relu', border_mode='valid', subsample=(1, 1), dim_ordering='tf', bias=True)\n",
    "bn_0 = BatchNormalization(mode=0, axis=-1, epsilon=1e-3)\n",
    "\n",
    "input_layer = Input(shape=data_in_shape)\n",
    "x = conv_0(input_layer)\n",
    "output_layer = bn_0(x)\n",
    "model = Model(input=input_layer, output=output_layer)\n",
    "\n",
    "np.random.seed(4000)\n",
    "data_in = 2 * np.random.random(data_in_shape) - 1\n",
    "\n",
    "# set weights to random (use seed for reproducibility)\n",
    "weights = []\n",
    "for i, w in enumerate(model.get_weights()):\n",
    "    np.random.seed(4000 + i)\n",
    "    if i == 5:\n",
    "        # std should be positive\n",
    "        weights.append(np.random.random(w.shape))\n",
    "    else:\n",
    "        weights.append(2 * np.random.random(w.shape) - 1)\n",
    "model.set_weights(weights)\n",
    "\n",
    "result = model.predict(np.array([data_in]))\n",
    "\n",
    "print({\n",
    "    'input': {'data': format_decimal(data_in.ravel().tolist()), 'shape': list(data_in_shape)},\n",
    "    'weights': [{'data': format_decimal(weights[i].ravel().tolist()), 'shape': list(weights[i].shape)} for i in range(len(weights))],\n",
    "    'expected': {'data': format_decimal(result[0].ravel().tolist()), 'shape': list(result[0].shape)}\n",
    "})"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "anaconda-cloud": {},
  "kernelspec": {
   "display_name": "Python [default]",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.5.2"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
